/**
 * RestTemplateTester.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * 2015年12月25日
 */
package com.ovt.order.test;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.User;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.ovt.common.exception.OVTException;
import com.ovt.common.model.JsonDocument;
import com.ovt.common.utils.JsonUtils;
import com.ovt.order.dao.vo.Order;

import junit.framework.TestCase;

/**
 * RestTemplateTester
 * 
 * @Author brad.zhou
 * @Version 1.0
 * @See 
 * @Since [OVT Cloud Platform]/[API] 1.0
 */
public class RestTemplateTester extends TestCase
{
    private RestTemplate restTemplate = new RestTemplate();
    
    private static final String BASE_URL = "http://localhost:18080/PaymentService/api"; 
    
    public void testPostJson() throws Exception
    {
        String url = BASE_URL + "/test/userJson";
        
        String data = URLEncoder.encode("user={\"nickName\": \"Rick\"}", "UTF-8");
        
        System.out.println(data);
        
        
        JsonDocument result = restTemplate.postForObject(url, data, JsonDocument.class);
        System.out.println(result);
    }
    
    public void testPostForEntity()
    {
        String url = BASE_URL + "/test/bye";
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        
        String data = "user={\"nickNam}\" : \"Rick\"}";
        HttpEntity<String> entity = new HttpEntity<String>(data, headers);
        
        List messageConverters = new ArrayList();
        messageConverters.add(new MappingJackson2HttpMessageConverter());
        restTemplate.setMessageConverters(messageConverters);
        JsonDocument result = restTemplate.postForObject(url, entity, JsonDocument.class);
        
        System.out.println(result);
        
        try
        {
            Order order = JsonUtils.fromJson(data, Order.class);
            System.out.println(order);
        }
        catch (OVTException e)
        {
            e.printStackTrace();
        }
    }
    
}
